package com.ostrich.project.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.util.HtmlUtil;
import com.base.web.BaseAction;
import com.google.gson.Gson;
import com.ostrich.common.Ganttbean;
import com.ostrich.manpower.bean.TostHrDepartinfo;
import com.ostrich.manpower.common.OstDateUtils;
import com.ostrich.manpower.service.impl.TostHrDepartinfoService;
import com.ostrich.project.bean.TostPjProjcttab;
import com.ostrich.project.bean.TostPjTeammemberinfo;
import com.ostrich.project.service.impl.TostPjProjcttabService;
import com.ostrich.project.service.impl.TostPjTeammemberinfoService;
import com.ostrich.system.analysis.ParaminfoUtil;
import com.ostrich.system.bean.TostSysParaminfo;

/**
 * ClassName: Controller <br/>
 * date: 2013-5-9 下午03:07:38 <br/>
 * 
 * @author wangyinan
 * @version
 */
@Controller
@RequestMapping("/tostpjteamdetail")
public class TostPjTeamDetailController extends BaseAction {
	private final static Logger log = Logger
			.getLogger(TostPjTeamDetailController.class);

	// Servrice start
	@Autowired(required = false)
	// 自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostPjTeammemberinfoService<TostPjTeammemberinfo> tostPjTeammemberinfoService;

	@Autowired(required = false)
	private TostPjProjcttabService<TostPjProjcttab> tostpjprojcttabservice;
	
	@Autowired(required = false)
	private TostHrDepartinfoService<TostHrDepartinfo> tosthrdepartinfoservice;
	/**
	 * ilook 首页
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(TostPjTeammemberinfo tostPjTeammemberinfo,
			HttpServletRequest request) throws Exception {
		Map<String, Object> context = getRootMap();
		List<TostPjTeammemberinfo> dataList = tostPjTeammemberinfoService
				.queryByList(tostPjTeammemberinfo);
		
		TostPjProjcttab tpj = tostpjprojcttabservice.queryById(tostPjTeammemberinfo.getPjId());
		TostHrDepartinfo thf = tosthrdepartinfoservice.queryById(tostPjTeammemberinfo.getHdpId());
		Gson gson = new Gson();
		// 设置页面数据
		context.put("jdata", gson.toJson(transfrom(dataList)));
		context.put("depart",thf);
		context.put("project",tpj);
		return forword("gantt/gantt", context);
	}

	/****
	 * 转化成gantt图像对象
	 * @param pjteam
	 * @return
	 */
	private List<Ganttbean> transfrom(List<TostPjTeammemberinfo> pjteam) {
		List<Ganttbean> gtls = new ArrayList<Ganttbean>();
		Map<Integer,TostSysParaminfo> statuspm = ParaminfoUtil.getParamByPid(99);
		if (pjteam != null && pjteam.size()>0) {
			for(TostPjTeammemberinfo pj:pjteam){
				Ganttbean gt = new Ganttbean();
				String editName = "<a href='javascript:void(0)' onclick='editperson("+pj.getId()+")'>"+pj.getHrName()+"</a>";
				gt.setName(editName);//第一个名字
				gt.setDesc(pj.getHdpName());//部门名称
				String startime =pj.getPtmEntertime();
				startime=(startime!=null&&startime.length()>0)?startime:"";
				String endtime = pj.getPtmEntertime();
				endtime=(endtime!=null&&endtime.length()>0)?endtime:"";
				List<Ganttbean.GanttValues> lsval = new ArrayList<Ganttbean.GanttValues>();
				Ganttbean.GanttValues vals = new Ganttbean().new GanttValues() ;
				Integer pdtats = (pj.getPtmStatus()==null)?0:pj.getPtmStatus();
				String pfin =(statuspm.get(pdtats)==null || statuspm.get(pdtats).getPaName()==null)?"":"<br>"+statuspm.get(pdtats).getPaName();
				
				vals.setId(pj.getId().toString());
				String fromtoMsg = "<br/>从"+startime+"到";
				
				Date dt =OstDateUtils.stringToDate(pj.getPtmEntertime());
				vals.setFrom("/Date("+Long.toString(dt.getTime())+")/");
				if(pj.getPtmLeavetime()==null||pj.getPtmLeavetime().length()<=0){
					dt=new Date();
					fromtoMsg+="至今";
				}else{
					dt =OstDateUtils.stringToDate(pj.getPtmLeavetime());
					fromtoMsg+=endtime;
				}
				vals.setTo("/Date("+Long.toString(dt.getTime())+")/");
				
				vals.setDesc(pj.getHrName()+pfin+fromtoMsg);//悬浮窗
				
				lsval.add(vals);
				
				gt.setValues(lsval);
				gtls.add(gt);
			}
		}
		return gtls;
	}

	/**
	 * json 列表页面
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/dataList")
	public void dataList(TostPjTeammemberinfo tostPjTeammemberinfo,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Integer pjId = (Integer) request.getSession().getAttribute("pjId");
		Integer hdpId = (Integer) request.getSession().getAttribute("hdpId");
		List<TostPjTeammemberinfo> dataList = tostPjTeammemberinfoService
				.queryByList(tostPjTeammemberinfo);

		// 设置页面数据
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("total", (dataList != null) ? dataList.size() : 0);
		jsonMap.put("data", dataList);
		HtmlUtil.writerJson(response, jsonMap);

	}

	/**
	 * 添加或修改数据
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public void save(TostPjTeammemberinfo bean, HttpServletResponse response)
			throws Exception {
		tostPjTeammemberinfoService.updateBySelective(bean);
		sendSuccessMessage(response, "保存成功~");
	}
	@RequestMapping("/edit")
	public ModelAndView edit(TostPjTeammemberinfo bean, HttpServletResponse response)
			throws Exception {
		tostPjTeammemberinfoService.updateInfo(bean);
		sendSuccessMessage(response, "更新成功~");
		return new ModelAndView("forward:/tostPjTeaminfo/list.do");
	}

	@RequestMapping("/getId")
	public void getId(Integer id, HttpServletResponse response)
			throws Exception {
		Map<String, Object> context = getRootMap();
		TostPjTeammemberinfo bean = tostPjTeammemberinfoService.queryById(id);
		if (bean == null) {
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", bean);
		HtmlUtil.writerJson(response, context);
	}

	@RequestMapping("/delete")
	public void delete(Integer[] id, HttpServletResponse response)
			throws Exception {
		tostPjTeammemberinfoService.delete(id);
		sendSuccessMessage(response, "删除成功");
	}

	
	@RequestMapping("/openperson")
	public ModelAndView openperson(TostPjTeammemberinfo tostPjTeammemberinfo, HttpServletResponse response)
			throws Exception {
		Map<String, Object> context = getRootMap();
		tostPjTeammemberinfo = tostPjTeammemberinfoService.queryById(tostPjTeammemberinfo.getId());
		context.put("teamperson",tostPjTeammemberinfo);
		return forword("project/tabTostPjTeamEditperson", context);
	}
}

